package com.orange.core.util;

import com.orange.core.security.detail.AdminDetail;
import com.orange.core.security.detail.UserDetail;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

public class PrincipalUtil {

    /**
     * 手动登录
     */
    public static void manualLogin(Authentication authentication) {
        SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
        securityContext.setAuthentication(authentication);
        SecurityContextHolder.setContext(securityContext);
    }

    public static Authentication getAuthentication() {
        return Optional.ofNullable(SecurityContextHolder.getContext()).map(SecurityContext::getAuthentication).orElse(null);
    }

    public static Object getPrincipal() {
        return Optional.ofNullable(SecurityContextHolder.getContext()).map(SecurityContext::getAuthentication).map(Authentication::getPrincipal).orElse(null);
    }

    public static Integer getId() {
        Object principal = getPrincipal();
        if (principal != null) {
            if (principal instanceof AdminDetail adminDetail) {
                return adminDetail.getId();
            } else if (principal instanceof UserDetail userDetail) {
                return userDetail.getId();
            }
        }
        return null;
    }

    public static boolean isUser() {
        Object principal = getPrincipal();
        if (principal != null) {
            return principal instanceof UserDetail;
        }
        return false;
    }

    public static boolean isAdmin() {
        Object principal = getPrincipal();
        if (principal != null) {
            return principal instanceof AdminDetail;
        }
        return false;
    }

    public static String getAdminName() {
        Object principal = getPrincipal();
        if (principal instanceof AdminDetail adminDetail) {
            return adminDetail.getUsername();
        }
        return null;
    }

    public static List<String> getAdminRoles() {
        Object principal = getPrincipal();
        if (principal instanceof AdminDetail adminDetail) {
            return adminDetail.getRoles();
        }
        return Collections.emptyList();
    }

    public static String getName() {
        Object principal = getPrincipal();
        if (principal != null) {
            if (principal instanceof AdminDetail adminDetail) {
                return adminDetail.getUsername();
            } else if (principal instanceof UserDetail userDetail) {
                return userDetail.getUsername();
            }
        }
        return null;
    }

    public static boolean isAuthenticated() {
        return Optional.ofNullable(SecurityContextHolder.getContext()).map(SecurityContext::getAuthentication).map(Authentication::isAuthenticated).orElse(false);
    }
}
